
namespace Pixels.Core
{
    using System;
    using System.Collections.Generic;
    using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
    using Pixels.ContentTypes;
    using Pixels.ContentTypes.DataLayer;
    using Pixels.Exceptions;

    internal class ContentTypeAdapter
    {
        #region private variables
        private DataAccessLayer dataLayer;
        private static object synclock = new object();
        private static ContentTypeAdapter _contenttypeadapter = null; 
        #endregion

        #region Constructors
        private ContentTypeAdapter()
        {
            dataLayer = new DataAccessLayer();
        }
        #endregion

        #region Methods

        /// <summary>
        /// Get Singleton Instance 
        /// </summary>
        /// <returns>Instance of ContentTypeAdapter</returns>
        public static ContentTypeAdapter GetInstance()
        {
            lock (synclock) // thread safe
            {
                if (_contenttypeadapter == null)
                {
                    _contenttypeadapter = new ContentTypeAdapter();
                }
            }
            return _contenttypeadapter;
        }

        /// <summary>
        /// IContentType Add/Edit
        /// </summary>
        /// <param name="objContentType">ContentType Object</param>
        /// <returns>IContentType</returns>
        public IContentType ContentTypeAddEdit(IContentType objContentType, bool updateifExists)
        {
            try
            {
                return dataLayer.ContentTypeAddEdit(objContentType, updateifExists);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ContentType could not be added/modified due to invalid data/technical issues.");
            }
        }

        /// <summary>
        /// Delete ContentType by ContentTypeID
        /// </summary>
        /// <param name="ContentTypeID">ContentTypeID</param>
        /// <returns></returns>
        public void ContentTypeDelete(Guid ContentTypeID)
        {
            try
            {
                dataLayer.ContentTypeDelete(ContentTypeID);
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("ContentType could not be deleted/Invalid ActivityID.");
            }
        }

        public IContentType ContentTypeFetch(Guid ContentTypeID)
        {
            try
            {
                IContentType objContentType = dataLayer.ContentTypeFetch(ContentTypeID);
                return objContentType;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                return null;
            }
        }

        public IContentType ContentTypeFetch(string ContentType)
        {
            try
            {
                IContentType objContentType = dataLayer.ContentTypeFetch(ContentType);
                return objContentType;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                return null;
            }
        }

        public IContentType ContentTypeFetchByListID(Guid ListID)
        {
            try
            {
                IContentType objContentType = dataLayer.ContentTypeFetch(ListID);
                return objContentType;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                return null;
            }
        }

        public IContentType ContentTypeFetchByContentTypeItemID(Guid ContentItemID)
        {
            try
            {
                IContentType objContentType = dataLayer.ContentTypeFetchByContentTypeItemID(ContentItemID);
                return objContentType;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                return null;
            }
        }

        public IList<IContentType> ContentTypesFetch()
        {
            try
            {
                IList<IContentType> ContentTypes = dataLayer.ContentTypesFetch();
                return ContentTypes;
            }
            catch (DLWrappedException Ex)
            {
                throw Ex;
            }
            catch (Exception Ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(Ex, "Business Layer Policy");
                throw new BLWrappedException("Error in Fetching ContentType Detail.");
            }
        }

        #endregion
    }
}
